package com.jhgsys.internal.workflow.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.constant.DictCodeConstant;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.common.utils.SpringContextUtil;
import com.jhgsys.internal.dict.entity.Dict;
import com.jhgsys.internal.dict.service.IDictService;
import com.jhgsys.internal.workflow.dto.WorkflowApplyDto;
import com.jhgsys.internal.workflow.entity.WorkflowApply;
import com.jhgsys.internal.workflow.service.IWorkflowApplyService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 流程申请表 Controller
 *
 * @author fenghaijun
 * @date 2024-07-19 13:53:28
 */
@Slf4j
@Validated
@Controller
public class WorkflowApplyController extends BaseController {

    @Autowired
    private IWorkflowApplyService workflowApplyService;

    @Autowired
    private IDictService dictService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "workflowApply")
    public String workflowApplyIndex(Model model,String types){
        String type="1";
        model.addAttribute("pageType","1");
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("peccancyRootCauseList", dictService.findDictByCode(DictCodeConstant.SAFETY_PECCANCY_ROOT_REASON));
        model.addAttribute("peccancyTypeList", dictService.findDictByCode(DictCodeConstant.SAFETY_PECCANCY_TYPE));
        if(("1").equals(type)){

        }
        return FebsUtil.view("workflow/workflowApply/workflowApplyList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "workflowApply/add")
    @RequiresPermissions("workflowApply:view")
    public String workflowApplyAdd(){
        return FebsUtil.view("workflow/workflowApply/workflowApplyEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "workflowApply/update/{id}")
    @RequiresPermissions("workflowApply:view")
    public String workflowApplyUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("workflowApply", workflowApplyService.findWorkflowApplyById(id));
        return FebsUtil.view("workflow/workflowApply/workflowApplyEdit");
    }

    /**
     * 查询（所有）
     * @author fenghaijun
     * @date 2024-07-19 13:53:28
     */
    @GetMapping("workflowApply")
    @ResponseBody
    @RequiresPermissions("workflowApply:view")
    public FebsResponse getAllWorkflowApplys(WorkflowApply workflowApply) {
        return new FebsResponse().success().data(workflowApplyService.findWorkflowApplys(workflowApply));
    }

    /**
     * 查询（分页）
     * @author fenghaijun
     * @date 2024-07-19 13:53:28
     */
    @GetMapping("workflowApply/list")
    @ResponseBody
    @RequiresPermissions("workflowApply:view")
    public FebsResponse workflowApplyList(QueryRequest request, WorkflowApply workflowApply) {
        Map<String, Object> dataTable = getDataTable(this.workflowApplyService.findWorkflowApplys(request, workflowApply));
        return new FebsResponse().success().data(dataTable);
    }



    /**
     * 逻辑删除 WorkflowApply
     * @author fenghaijun
     * @date 2024-07-19 13:53:28
     */
    @Log("逻辑删除WorkflowApply")
    @GetMapping("workflowApply/delete/{id}")
    @ResponseBody
    @RequiresPermissions("workflowApply:view")
    public FebsResponse deleteWorkflowApply(@PathVariable("id") String workflowApplyIds) throws FebsException {
        try {
            WorkflowApply workflowApply = new WorkflowApply();
            workflowApply.setDeletedBy(getCurrentUser().getUserName());
            workflowApply.setDeletedTime(new Date());
            workflowApply.setDeletedFlg( FebsConstant.INVALID);
            this.workflowApplyService.deleteWorkflowApply(workflowApply,workflowApplyIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除WorkflowApply失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 WorkflowApply
    * @author fenghaijun
    * @date 2024-07-19 13:53:28
    */
    @Log("物理删除WorkflowApply")
    @GetMapping("workflowApply/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("workflowApply:view")
    public FebsResponse physicallyDeleteWorkflowApply(@PathVariable("id") String workflowApplyIds) throws FebsException {
        try {
            this.workflowApplyService.physicallyDeleteWorkflowApply(workflowApplyIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除WorkflowApply失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }


    /**
     * 导出 WorkflowApply的excel
     * @author fenghaijun
     * @date 2024-07-19 13:53:28
     */
    @PostMapping("workflowApply/excel")
    @ResponseBody
    @RequiresPermissions("workflowApply:view")
    public void export(QueryRequest queryRequest, WorkflowApply workflowApply, HttpServletResponse response) throws FebsException {
        try {
            List<WorkflowApply> workflowApplys = this.workflowApplyService.findWorkflowApplys(queryRequest, workflowApply).getRecords();
            ExcelKit.$Export(WorkflowApply.class, response).downXlsx(workflowApplys, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 导出 WorkflowApply的excel
     * @author fenghaijun
     * @date 2024-07-19 13:53:28
     */
    @GetMapping("test/workflowTask")
    @ResponseBody
    public FebsResponse workflowTaskTest() throws FebsException {
        try {
            Object[] args = new Object[]{"workflowApplyId"};
            Class<?>[] argsType = new Class<?>[]{String.class};
            Class<?> clazz = SpringContextUtil.getType("test1");
            Object instance = clazz.newInstance();
            Method method = clazz.getMethod("execute", argsType);
            method.invoke(instance,args);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 查询（分页）
     *
     * @author chenweihua
     * @date 2020-05-22 15:28:25
     */
    @GetMapping("workflowApply/findApplyAndPeccancy")
    @ResponseBody
    public FebsResponse findApplyAndPeccancy(QueryRequest request, WorkflowApplyDto workflowApply) {
        workflowApply.setCurrentUserId(getCurrentUserId());
        Map<String, Object> dataTable = getDataTable(workflowApplyService.findApplyAndPeccancy(request, workflowApply));
        return new FebsResponse().success().data(dataTable);
    }
}
